package com.huawei.hwCloudJs.service.hms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.api.HuaweiApiClient.ConnectionCallbacks;
import com.huawei.hms.api.HuaweiApiClient.OnConnectionFailedListener;
import com.huawei.hms.api.HuaweiApiClientImpl;
import com.huawei.hms.support.api.client.BundleResult;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.api.hwid.SignInResult;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwCloudJs.core.JsCallback;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class b {
    public static final int a = 9003;
    public static final int b = 1;
    private static final String c = "HmsCoreApiHelper";
    private static final int d = 9004;

    static final class a implements com.huawei.hwCloudJs.service.hms.a.c {
        private String a;
        private JsCallback b;

        public a(String str, JsCallback jsCallback) {
            this.a = str;
            this.b = jsCallback;
        }

        public void a(com.huawei.hwCloudJs.service.hms.a.b bVar) {
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("resultCode", bVar.b());
            } catch (JSONException e) {
                Log.e(b.c, "JS getResultFromActivityResult put json error");
            }
            Intent a = bVar.a();
            if (a != null) {
                Bundle extras = a.getExtras();
                if (extras != null) {
                    e a2 = f.a(2);
                    JSONObject jSONObject2 = new JSONObject();
                    a2.a(this.a, extras, jSONObject2);
                    try {
                        jSONObject.put(HiAnalyticsConstant.BI_KEY_RESUST, jSONObject2);
                    } catch (JSONException e2) {
                        Log.e(b.c, "JS InvokeResultCallback onResult JSONException");
                        this.b.failure();
                    }
                }
            }
            this.b.success(jSONObject.toString());
        }
    }

    public static final class b implements OnConnectionFailedListener {
        private WeakReference<HuaweiApiClient> a;
        private JsCallback b;
        private WeakReference<Activity> c;

        static final class a implements com.huawei.hwCloudJs.service.hms.a.c {
            private static final String a = "intent.extra.RESULT";
            private WeakReference<HuaweiApiClient> b;
            private WeakReference<Activity> c;
            private JsCallback d;

            public a(WeakReference<HuaweiApiClient> weakReference, WeakReference<Activity> weakReference2, JsCallback jsCallback) {
                this.b = weakReference;
                this.c = weakReference2;
                this.d = jsCallback;
            }

            public void a(com.huawei.hwCloudJs.service.hms.a.b bVar) {
                int i = -1;
                if (bVar != null && bVar.b() == -1) {
                    Intent a = bVar.a();
                    if (a != null) {
                        i = a.getIntExtra("intent.extra.RESULT", -1);
                    }
                }
                Log.e(b.c, "JS ConnFailResult result " + i);
                if (i == 0) {
                    HuaweiApiClient huaweiApiClient = (HuaweiApiClient) this.b.get();
                    if (huaweiApiClient != null && !huaweiApiClient.isConnecting() && !huaweiApiClient.isConnected()) {
                        Log.i(b.c, "JS ConnFailResult connect ");
                        if (this.c != null) {
                            huaweiApiClient.connect((Activity) this.c.get());
                            return;
                        } else {
                            huaweiApiClient.connect(null);
                            return;
                        }
                    }
                    return;
                }
                String str = i + "";
                Log.e(b.c, "JS ConnFailResult onConnectionFailed  " + str);
                this.d.failure(str);
            }
        }

        public b(WeakReference<HuaweiApiClient> weakReference, JsCallback jsCallback, WeakReference<Activity> weakReference2) {
            this.a = weakReference;
            this.b = jsCallback;
            this.c = weakReference2;
        }

        public void onConnectionFailed(ConnectionResult connectionResult) {
            final int errorCode = connectionResult.getErrorCode();
            final HuaweiApiAvailability instance = HuaweiApiAvailability.getInstance();
            if (instance.isUserResolvableError(errorCode)) {
                Log.e(b.c, "JS InvokeResultCallback onConnectionFailed REQUEST_CODE_CONN_ERROR");
                a.a().a(9004, new a(this.a, this.c, this.b));
                new Handler(Looper.getMainLooper()).post(new Runnable(this) {
                    final /* synthetic */ b c;

                    public void run() {
                        Activity activity = (Activity) this.c.c.get();
                        if (activity != null) {
                            instance.resolveError(activity, errorCode, 9004);
                        }
                    }
                });
                return;
            }
            String str = errorCode + "";
            Log.e(b.c, "JS InvokeResultCallback onConnectionFailed errorCode " + str);
            this.b.failure(str);
        }
    }

    public static final class c implements ConnectionCallbacks {
        private WeakReference<HuaweiApiClient> a;
        private JsCallback b;
        private String c;
        private JSONObject d;

        public c(WeakReference<HuaweiApiClient> weakReference, JsCallback jsCallback, String str, JSONObject jSONObject) {
            this.a = weakReference;
            this.b = jsCallback;
            this.c = str;
            this.d = jSONObject;
        }

        public void onConnected() {
            b.a(this.a, this.c, this.d, this.b);
        }

        public void onConnectionSuspended(int i) {
        }
    }

    static final class d implements ResultCallback<BundleResult> {
        private WeakReference<HuaweiApiClient> a;
        private JsCallback b;

        public d(WeakReference<HuaweiApiClient> weakReference, JsCallback jsCallback) {
            this.a = weakReference;
            this.b = jsCallback;
        }

        public void a(BundleResult bundleResult) {
            HuaweiApiClient huaweiApiClient = (HuaweiApiClient) this.a.get();
            if (huaweiApiClient == null) {
                this.b.failure();
                return;
            }
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("statusCode", bundleResult.getResultCode());
                if (bundleResult.getRspBody() != null) {
                    JSONObject jSONObject2 = new JSONObject();
                    f.a(2).a(huaweiApiClient.getAppID(), bundleResult.getRspBody(), jSONObject2);
                    jSONObject.put(HiAnalyticsConstant.BI_KEY_RESUST, jSONObject2);
                }
                this.b.success(jSONObject.toString());
            } catch (JSONException e) {
                Log.e(b.c, "InvokeResultCallback JSONException");
                this.b.failure();
            }
        }

        public /* synthetic */ void onResult(Object obj) {
            a((BundleResult) obj);
        }
    }

    public static String a(SignInResult signInResult, boolean z) {
        JSONObject jSONObject = new JSONObject();
        if (z) {
            try {
                jSONObject.put("serverAuthCode", signInResult.getSignInHuaweiId().getServerAuthCode());
            } catch (JSONException e) {
                Log.e(c, "signInRes2Json put json error");
            }
        } else {
            jSONObject.put("openId", signInResult.getSignInHuaweiId().getOpenId());
            jSONObject.put("accessToken", signInResult.getSignInHuaweiId().getAccessToken());
            jSONObject.put("displayName", signInResult.getSignInHuaweiId().getDisplayName());
            jSONObject.put("photoUrl", signInResult.getSignInHuaweiId().getPhotoUrl());
        }
        return jSONObject.toString();
    }

    public static void a(String str, JsCallback jsCallback) {
        a.a().a(9003, new a(str, jsCallback));
    }

    public static void a(WeakReference<HuaweiApiClient> weakReference, String str, JSONObject jSONObject, JsCallback jsCallback) {
        HuaweiApiClient huaweiApiClient = (HuaweiApiClient) weakReference.get();
        if (huaweiApiClient == null) {
            jsCallback.failure();
            return;
        }
        ((HuaweiApiClientImpl) huaweiApiClient).asyncRequest(f.a(2).a(jSONObject, new Bundle()), str, 2, new d(weakReference, jsCallback));
    }

    public static boolean a(List<Scope> list, List<Scope> list2) {
        List arrayList = new ArrayList();
        if (list != null) {
            for (Scope scope : list) {
                if (!(scope == null || arrayList.contains(scope.getScopeUri()))) {
                    arrayList.add(scope.getScopeUri());
                }
            }
        }
        List arrayList2 = new ArrayList();
        if (list2 != null) {
            for (Scope scope2 : list2) {
                if (!(scope2 == null || arrayList2.contains(scope2.getScopeUri()))) {
                    arrayList2.add(scope2.getScopeUri());
                }
            }
        }
        Collections.sort(arrayList);
        Collections.sort(arrayList2);
        return arrayList.equals(arrayList2);
    }
}
